home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / misc / muibuilderv20.lha / MUIBuilder / MB / Developer / C / Sources_GenCodeC / GenCodeC.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-18  |  19.7 KB  |  772 lines

  1. #include "gencodec.h"
  2.  
  3. char *version = "$VER: GenCodeC 2.0 (20-05-94)";
  4.  
  5. struct Library * MUIBBase = NULL;
  6.  
  7. /* Global variables */
  8. ULONG    varnb;            /* number of variables */
  9.  
  10. BOOL    Code, Env;        /* flags-options */
  11. BOOL    Locale, Declarations;
  12. BOOL    Notifications;
  13. BOOL    ExternalExist = FALSE;
  14. char    *FileName, *CatalogName;/* Strings */
  15. char    *GetString;
  16. char    *GetMBString;
  17.  
  18. FILE    *file;
  19.  
  20. char    HeaderFile[512];
  21. char    GUIFile[512];
  22. char    MBDir[512];
  23. char    Externals[512];
  24. char    Main[512];
  25.  
  26. /* variable types */
  27. char    *STR_type[] =
  28.     {
  29.         "BOOL",
  30.         "int",
  31.         "char *",
  32.         "char *",
  33.         "APTR",
  34.         "",
  35.         "",
  36.         "",
  37.         "APTR"
  38.     };
  39.  
  40. void Indent(int nb)
  41. {
  42.         int     i;
  43.  
  44.         for(i=0;i<nb;i++) fprintf(file, "\t");
  45. }
  46.  
  47. void WriteDeclarations(int vartype)
  48. {
  49.     int    i;
  50.     char    *varname;
  51.     ULONG    type, size;
  52.     char    *typename;
  53.     int    nb_ident = 1;
  54.     char    buffer[150];
  55.     
  56.     typename = STR_type[ vartype - 1 ];        /* find the name 'BOOL ...'    */
  57.     buffer[0] = '\0';
  58.     for(i=0;i<varnb;i++)
  59.     {
  60.         MB_GetVarInfo (i,
  61.                    MUIB_VarType, &type,
  62.                    MUIB_VarName, &varname,
  63.                    MUIB_VarSize, &size,
  64.                 TAG_END
  65.                  );
  66.         if (type == vartype)
  67.             switch(type)
  68.             {
  69.             case TYPEVAR_TABSTRING:
  70.                 fprintf(file, "\t%s\t%s[%d];\n",
  71.                      typename,
  72.                      varname,
  73.                      size+1
  74.                     );
  75.                 break;
  76.             case TYPEVAR_IDENT:
  77.                 fprintf(file,"#define %s %d\n", varname, nb_ident++);
  78.                 break;
  79.             case TYPEVAR_LOCAL_PTR:
  80.                 if (strlen(buffer)==0) sprintf(buffer, "\t%s\t%s", typename, varname);
  81.                 else
  82.                 {
  83.                     strcat(buffer, ", ");
  84.                     strcat(buffer, varname);
  85.                 }
  86.                 if (strlen(buffer)>=70)
  87.                 {
  88.                     strcat(buffer, ";\n");
  89.                     fprintf(file, "%s", buffer);
  90.                     buffer[0] = '\0';
  91.                 }
  92.             break;
  93.             default:
  94.                 fprintf(file, "\t%s\t%s;\n",typename, varname);
  95.                 break;
  96.             }
  97.     }
  98.     if (strlen(buffer)>0) fprintf(file, "%s;\n", buffer);
  99. }
  100.  
  101. void WriteInitialisations(int vartype)
  102. {
  103.     int    i, j;
  104.     ULONG    type, size;
  105.     char    *inits, *name;
  106.     BOOL    enter = FALSE;
  107.  
  108.     for(i=0;i<varnb;i++)
  109.     {
  110.         MB_GetVarInfo(i,
  111.                    MUIB_VarType    , &type,
  112.                    MUIB_VarName    , &name,
  113.                    MUIB_VarSize    , &size,
  114.                    MUIB_VarInitPtr    , &inits,
  115.                    TAG_END
  116.                  );
  117.         if (type == vartype)
  118.         {
  119.             enter = TRUE;
  120.             switch(type)
  121.             {
  122.             case TYPEVAR_TABSTRING:
  123.                 for(j=0;j<size;j++)
  124.                 {
  125.                     if (!Locale) fprintf(file, "\tObject->%s[%d] = \"%s\";\n", name, j, inits);
  126.                     else        fprintf(file, "\tObject->%s[%d] = %s(%s);\n", name, j, GetMBString, inits);
  127.                     inits = inits + strlen(inits) + 1;
  128.                 }
  129.                 fprintf(file, "\tObject->%s[%d] = NULL;\n", name, j);
  130.                 break;
  131.             case TYPEVAR_STRING:
  132.                  if (*inits != 0)
  133.                 {
  134.                     if (!Locale) fprintf(file, "\tObject->%s = \"%s\";\n", name, inits);
  135.                     else       fprintf(file, "\tObject->%s = %s(%s);\n", name, GetMBString, inits);
  136.                 }
  137.                 else fprintf(file, "\tObject->%s = NULL;\n", name);
  138.                 break;
  139.             case TYPEVAR_HOOK:
  140.                 fprintf(file, "\tstatic const struct Hook %sHook = { { NULL,NULL },(VOID *)%s,NULL,NULL };\n", name, name);
  141.                 break;
  142.             default:
  143.                 break;
  144.             }
  145.         }
  146.     }
  147.     if (enter) fprintf(file, "\n");
  148. }
  149.  
  150. void WriteCode(void)
  151. {
  152.     ULONG    type;
  153.     char*    code;
  154.     BOOL    InFunction     = FALSE;
  155.     BOOL    IndentFunction = TRUE ;
  156.     BOOL    obj_function             ;
  157.     BOOL    InObj              ;
  158.     int    nb_indent      = 1    ;
  159.     int    nb_function    = 0    ;
  160.     int    name;
  161.  
  162.     extern void End(void);
  163.  
  164.     MB_GetNextCode(&type, &code);
  165.     while(type != -1)
  166.     {
  167.         switch(type)
  168.         {
  169.         case TC_CREATEOBJ:
  170.             name = atoi(code);
  171.             fprintf(file, "%s,\n",MUIStrings[name]);
  172.             nb_indent++;
  173.             IndentFunction = TRUE;
  174.             MB_GetNextCode(&type, &code);
  175.             InObj = TRUE;
  176.             break;
  177.         case TC_ATTRIBUT:
  178.             Indent(nb_indent);
  179.             name = atoi(code);
  180.             fprintf(file, "%s, ",MUIStrings[name]);
  181.             IndentFunction = FALSE;
  182.             MB_GetNextCode(&type, &code);
  183.             break;
  184.         case TC_END:
  185.             nb_indent--;
  186.             InObj = FALSE;
  187.             Indent(nb_indent);
  188.             name = atoi(code);
  189.             fprintf(file, "%s",MUIStrings[name]);
  190.             IndentFunction = TRUE;
  191.             MB_GetNextCode(&type, &code);
  192.             fprintf(file, ";\n\n");
  193.             break;
  194.         case TC_MUIARG_OBJFUNCTION:
  195.             if (IndentFunction) Indent(nb_indent);
  196.             nb_function++;
  197.             name = atoi(code);
  198.             fprintf(file, "%s(",MUIStrings[name]);
  199.             IndentFunction = FALSE;
  200.             InFunction     = TRUE;
  201.             MB_GetNextCode(&type, &code);
  202.             obj_function = TRUE;
  203.             break;
  204.         case TC_MUIARG_FUNCTION:
  205.         case TC_FUNCTION:
  206.             if (IndentFunction) Indent(nb_indent);
  207.             nb_function++;
  208.             name = atoi(code);
  209.             fprintf(file, "%s(",MUIStrings[name]);
  210.             IndentFunction = FALSE;
  211.             InFunction     = TRUE;
  212.             MB_GetNextCode(&type, &code);
  213.             obj_function = FALSE;
  214.             break;
  215.         case TC_OBJFUNCTION:
  216.             if (IndentFunction) Indent(nb_indent);
  217.             nb_function++;
  218.             name = atoi(code);
  219.             fprintf(file, "%s(",MUIStrings[name]);
  220.             InFunction     = TRUE;
  221.             IndentFunction = FALSE;
  222.             MB_GetNextCode(&type,&code);
  223.             obj_function = TRUE;
  224.             break;
  225.         case TC_STRING:
  226.             fprintf(file, "\"%s\"",code);
  227.             MB_GetNextCode(&type, &code);
  228.             IndentFunction = TRUE;
  229.             if (InFunction)
  230.             {
  231.                 if (type  != TC_END_FUNCTION) fprintf(file, ", ");
  232.                 IndentFunction = FALSE;
  233.             }
  234.             else    fprintf(file, ",\n");
  235.             break;
  236.         case TC_LOCALESTRING:
  237.             fprintf(file, "%s(%s)",GetMBString, code);
  238.                         MB_GetNextCode(&type, &code);
  239.                         IndentFunction = TRUE;
  240.                         if (InFunction)
  241.                         {
  242.                                 if (type  != TC_END_FUNCTION) fprintf(file, ", ");
  243.                                 IndentFunction = FALSE;
  244.                         }
  245.                         else    fprintf(file, ",\n");
  246.                         break;
  247.         case TC_LOCALECHAR:
  248.             fprintf(file, "%s(%s)[0]",GetString, code);
  249.                         MB_GetNextCode(&type, &code);
  250.                         IndentFunction = TRUE;
  251.                         if (InFunction)
  252.                         {
  253.                                 if (type  != TC_END_FUNCTION) fprintf(file, ", ");
  254.                                 IndentFunction = FALSE;
  255.                         }
  256.                         else    fprintf(file, ",\n");
  257.                         break;
  258.         case TC_INTEGER:
  259.             fprintf(file, "%s", code);
  260.                         MB_GetNextCode(&type, &code);
  261.                         IndentFunction = TRUE;
  262.                         if (InFunction)
  263.                         {
  264.                                 if (type  != TC_END_FUNCTION) fprintf(file, ", ");
  265.                                 IndentFunction = FALSE;
  266.                         }
  267.                         else    fprintf(file, ",\n");
  268.                         break;
  269.         case TC_CHAR:
  270.             fprintf(file, "'%s'",code);
  271.                         MB_GetNextCode(&type, &code);
  272.                         IndentFunction = TRUE;
  273.                         if (InFunction)
  274.                         {
  275.                                 if (type  != TC_END_FUNCTION) fprintf(file, ", ");
  276.                                 IndentFunction = FALSE;
  277.                         }
  278.                         else    fprintf(file, ",\n");
  279.                         break;    
  280.         case TC_VAR_AFFECT:
  281.             name = atoi(code);
  282.             MB_GetVarInfo(name, MUIB_VarName, &code, MUIB_VarType, &type, TAG_END);
  283.             if (type == TYPEVAR_LOCAL_PTR) fprintf( file, "\t%s = ", code);
  284.             else fprintf(file, "\tObject->%s = ", code); 
  285.             IndentFunction = FALSE;
  286.             MB_GetNextCode(&type, &code);
  287.             break;
  288.         case TC_OBJ_ARG:
  289.         case TC_VAR_ARG:
  290.             name = atoi(code);
  291.             MB_GetVarInfo(name, MUIB_VarName, &code, MUIB_VarType, &type, TAG_END);
  292.             if (type == TYPEVAR_LOCAL_PTR) fprintf(file, "%s", code);
  293.             else                   fprintf(file, "Object->%s", code);
  294.             MB_GetNextCode(&type, &code);
  295.             if ((InFunction)&&(type != TC_END_FUNCTION)) fprintf(file, ", ");
  296.             if (!InFunction)
  297.             {
  298.                 fprintf(file, ",\n");
  299.                 IndentFunction = TRUE;
  300.             }
  301.             break;
  302.         case TC_END_FUNCTION:
  303.             MB_GetNextCode(&type, &code);
  304.             if (nb_function>1)
  305.             {
  306.                 if (type != TC_END_FUNCTION) fprintf(file, "),");
  307.                 else                 fprintf(file, ")");
  308.             }    
  309.             else
  310.             {
  311.                 if (obj_function) fprintf(file, ");\n\n");
  312.                 else          fprintf(file, "),\n");
  313.                 IndentFunction = TRUE;
  314.                 InFunction     = FALSE;
  315.             }
  316.             nb_function--;
  317.             break;
  318.         case TC_BOOL:
  319.             if (*code == '0')    fprintf(file, "FALSE");
  320.             else            fprintf(file, "TRUE" );
  321.             MB_GetNextCode(&type, &code);
  322.             if (InFunction)
  323.             {
  324.                 if (type != TC_END_FUNCTION)
  325.                 {
  326.                     fprintf(file, ", ");
  327.                     IndentFunction = FALSE;
  328.                 }
  329.             }
  330.             else fprintf(file, ",\n");
  331.             break;
  332.         case TC_MUIARG:
  333.             if (IndentFunction) Indent(nb_indent);
  334.             name = atoi(code);
  335.             fprintf(file, "%s", MUIStrings[name]);
  336.             MB_GetNextCode(&type, &code);
  337.             if (InFunction)
  338.             {
  339.                 if (type != TC_END_FUNCTION)
  340.                 {
  341.                     fprintf(file, ", ");
  342.                     IndentFunction = FALSE;
  343.                 }
  344.             }
  345.             else
  346.             {
  347.                 fprintf(file, ",\n");
  348.                 IndentFunction = TRUE;
  349.             }
  350.             break;
  351.         case TC_MUIARG_ATTRIBUT:
  352.             if (IndentFunction) Indent(nb_indent);
  353.             name = atoi(code);
  354.             MB_GetNextCode(&type, &code);
  355.             if (InObj) fprintf(file, "%s,\n", MUIStrings[name]);
  356.             else
  357.             {
  358.                 if (InFunction)
  359.                 {
  360.                     if (type != TC_END_FUNCTION)
  361.                         fprintf(file, "%s,", MUIStrings[name]);
  362.                     else    fprintf(file, "%s", MUIStrings[name]);
  363.                 }
  364.                 else
  365.                 {
  366.                     fprintf(file, "%s;\n\n", MUIStrings[name]);
  367.                 }
  368.             }
  369.             break;
  370.         case TC_MUIARG_OBJ:
  371.             if (IndentFunction) Indent(nb_indent);
  372.             name = atoi(code);
  373.             MB_GetNextCode(&type, &code);
  374.             fprintf(file, "%s;\n\n", MUIStrings[name]);
  375.             break;
  376.         case TC_EXTERNAL_FUNCTION:
  377.             fprintf(file, "&%sHook", code);
  378.             MB_GetNextCode(&type, &code);
  379.             if (InFunction)
  380.             {
  381.                 if (type != TC_END_FUNCTION)    
  382.                 {
  383.                     fprintf(file, ", ");
  384.                     IndentFunction = FALSE;
  385.                 }
  386.             }
  387.             else
  388.             {
  389.                 fprintf(file, ",\n");
  390.                 IndentFunction = TRUE;
  391.             }
  392.             break;
  393.         default:
  394.             printf("Type = %d\n", type);
  395.             printf("ERROR !!!!! THERE IS A PROBLEM WITH THIS FILE !!!\n");
  396.             End();
  397.             exit(1);
  398.             break;
  399.         }
  400.     }
  401. }
  402.  
  403. void WriteNotify(void)
  404. {
  405.     ULONG    type;
  406.     char*    code;
  407.     int    name;
  408.     BOOL    indent = FALSE;
  409.  
  410.     extern void End(void);
  411.  
  412.     fprintf(file, "\n");
  413.     MB_GetNextNotify(&type, &code);
  414.     while(type != -1)
  415.     {
  416.         if (indent) fprintf(file, "\t\t");
  417.         indent = TRUE;
  418.         switch(type)
  419.         {
  420.         case TC_END_FUNCTION:
  421.         case TC_END_NOTIFICATION:
  422.             fprintf(file, ");\n\n");
  423.             MB_GetNextNotify(&type, &code);
  424.             indent = FALSE;
  425.             break;
  426.         case TC_BEGIN_NOTIFICATION:
  427.             name = atoi(code);
  428.             MB_GetVarInfo(name, MUIB_VarName, &code, TAG_END);
  429.             fprintf(file, "\tDoMethod(Object->%s,\n", code);
  430.             MB_GetNextNotify(&type, &code);
  431.             break;
  432.         case TC_FUNCTION:
  433.             name = atoi(code);
  434.             fprintf(file, "\t%s(", MUIStrings[name]);
  435.             MB_GetNextNotify(&type, &code);
  436.             indent = FALSE;
  437.             break;
  438.         case TC_STRING:
  439.             fprintf(file, "\"%s\"",code);
  440.             MB_GetNextNotify(&type, &code);
  441.             if ((type  != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION))  fprintf(file, ",\n");
  442.             else fprintf(file, "\n");
  443.             break;
  444.         case TC_LOCALESTRING:
  445.             fprintf(file, "%s(%s)",GetMBString, code);
  446.                         MB_GetNextNotify(&type, &code);
  447.                         if ((type  != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  448.             else fprintf(file, "\n");
  449.                         break;
  450.         case TC_LOCALECHAR:
  451.             fprintf(file, "%s(%s)[0]\n",GetString, code);
  452.                         MB_GetNextNotify(&type, &code);
  453.                         if ((type  != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  454.             else fprintf(file, "\n");
  455.                         break;
  456.         case TC_INTEGER:
  457.             fprintf(file, "%s", code);
  458.                         MB_GetNextNotify(&type, &code);
  459.                         if ((type  != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  460.             else fprintf(file, "\n");
  461.                         break;
  462.         case TC_CHAR:
  463.             fprintf(file, "'%s'",code);
  464.                         MB_GetNextNotify(&type, &code);
  465.                         if ((type  != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  466.             else fprintf(file, "\n");
  467.                         break;    
  468.         case TC_VAR_ARG:
  469.             name = atoi(code);
  470.             MB_GetVarInfo(name, MUIB_VarName, &code, TAG_END);
  471.             fprintf(file, "Object->%s", code);
  472.             MB_GetNextNotify(&type, &code);
  473.             if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  474.             else fprintf(file, "\n");
  475.             break;
  476.         case TC_BOOL:
  477.             if (*code == '0')    fprintf(file, "FALSE");
  478.             else            fprintf(file, "TRUE" );
  479.             MB_GetNextNotify(&type, &code);
  480.             if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  481.             else fprintf(file, "\n");
  482.             break;
  483.         case TC_MUIARG:
  484.         case TC_MUIARG_OBJ:
  485.             name = atoi(code);
  486.             fprintf(file, "%s", MUIStrings[name]);
  487.             MB_GetNextNotify(&type, &code);
  488.             if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ", ");
  489.             indent = FALSE;
  490.             break;
  491.         case TC_MUIARG_ATTRIBUT:
  492.             name = atoi(code);
  493.             fprintf(file, "%s", MUIStrings[name]);
  494.             MB_GetNextNotify(&type, &code);
  495.             if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  496.             else fprintf(file, "\n");
  497.             break;
  498.         case TC_EXTERNAL_CONSTANT:
  499.                         fprintf(file, "%s", code);
  500.                         MB_GetNextNotify(&type, &code);
  501.                         if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  502.             else fprintf(file, "\n");
  503.                         break;
  504.         case TC_EXTERNAL_FUNCTION:
  505.             fprintf(file, "&%sHook", code);
  506.             MB_GetNextNotify(&type, &code);
  507.                         if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  508.             else fprintf(file, "\n");
  509.             break;
  510.         case TC_EXTERNAL_VARIABLE:
  511.             fprintf(file, "%s", code);
  512.             MB_GetNextNotify(&type, &code);
  513.                         if ((type != TC_END_NOTIFICATION)&&(type != TC_END_FUNCTION)) fprintf(file, ",\n");
  514.             else fprintf(file, "\n");
  515.             break;
  516.         default:
  517.             printf("Type = %d\n", type);
  518.             printf("ERROR !!!!! THERE IS A PROBLEM WITH THIS FILE !!!\n");
  519.             End();
  520.             exit(1);
  521.             break;
  522.         }
  523.     }
  524. }
  525.  
  526. void Init(void)
  527. {
  528.     BPTR    lock;
  529.  
  530.     /* Get all needed variables */
  531.     MB_Get    (
  532.         MUIB_VarNumber        , &varnb,
  533.         MUIB_Code        , &Code,
  534.         MUIB_Environment    , &Env,
  535.         MUIB_Locale        , &Locale,
  536.         MUIB_Notifications    , &Notifications,
  537.         MUIB_Declarations    , &Declarations,
  538.         MUIB_FileName        , &FileName,
  539.         MUIB_CatalogName    , &CatalogName,
  540.         MUIB_GetStringName    , &GetString,
  541.         TAG_END
  542.         );
  543.  
  544.     /* Create 'GetMBString' name */
  545.     if (strcmp(GetString, "GetMBString") == 0) GetMBString  = "GetMBString2";
  546.     else                       GetMBString = "GetMBString";
  547.  
  548.     /* Create File Names */
  549.     remove_extend(FileName);
  550.     strncpy(GUIFile, FileName, 512);
  551.     add_extend(GUIFile, ".c");
  552.     strncpy(HeaderFile, FileName, 512);
  553.     add_extend(HeaderFile, ".h");
  554.     strncpy(Externals, FileName, 512);
  555.     strncat(Externals, "Extern", 512);
  556.     add_extend(Externals, ".h");
  557.     strncpy(Main, FileName, 512);
  558.     strncat(Main, "Main", 512);
  559.     add_extend(Main, ".c");
  560.  
  561.  
  562.     /* Get Current Directory Name */
  563.     lock = Lock("PROGDIR:", ACCESS_READ);
  564.     NameFromLock(lock, MBDir, 512);
  565.     UnLock(lock);
  566. }
  567.  
  568. void WriteHeaderFile(void)
  569. {
  570.     char    *name;
  571.     char    buffer[600];
  572.     char    buffer2[600];
  573.  
  574.     if (Env)
  575.     {
  576.         strncpy(buffer, MBDir, 600);
  577.         AddPart(buffer, "H-Header", 512);
  578.         sprintf(buffer2, "copy \"%s\" \"%s\"", buffer, HeaderFile);
  579.         Execute(buffer2,0,0);
  580.     }
  581.     else   DeleteFile(HeaderFile);
  582.     file = fopen(HeaderFile, "a+");
  583.     if (file)
  584.     {
  585.         MB_GetVarInfo(0, MUIB_VarName, &name, TAG_END);
  586.         fprintf(file, "struct Obj%s\n{\n", name);
  587.         WriteDeclarations(TYPEVAR_PTR        );
  588.         WriteDeclarations(TYPEVAR_BOOL       );
  589.         WriteDeclarations(TYPEVAR_INT        );
  590.         WriteDeclarations(TYPEVAR_STRING   );
  591.         WriteDeclarations(TYPEVAR_TABSTRING);
  592.         fprintf(file, "};\n\n");
  593.         if (Notifications)
  594.         {
  595.             WriteDeclarations(TYPEVAR_IDENT    );
  596.             fprintf(file, "\n");
  597.         }
  598.         if (Env)
  599.         {
  600.             fprintf(file,"extern struct Obj%s * Create%s(void);\n", name, name);
  601.             fprintf(file,"extern void Dispose%s(struct Obj%s *);\n", name, name);
  602.         }
  603.         fclose(file);
  604.     }
  605. }
  606.  
  607. void WriteGUIFile(void)
  608. {
  609.     char    buffer[600];
  610.     char    buffer2[600];
  611.     char    *name;
  612.  
  613.     if (Env)
  614.     {
  615.         strncpy(buffer, MBDir, 600);
  616.         AddPart(buffer, "C-Header", 512);
  617.         sprintf(buffer2, "copy \"%s\" \"%s\"", buffer, GUIFile);
  618.         Execute(buffer2,0,0);
  619.     }
  620.     else DeleteFile(GUIFile);
  621.     if (file = fopen(GUIFile, "a+"))
  622.     {
  623.         if (Env)
  624.         {
  625.             MB_GetVarInfo(0, MUIB_VarName, &name, TAG_END);
  626.             fprintf(file, "\n#include \"%s\"\n", FilePart(HeaderFile));
  627.             if (ExternalExist) fprintf(file, "#include \"%s\"\n\n", FilePart(Externals));
  628.             if (Locale)
  629.             {
  630.                 remove_extend( CatalogName );
  631.                 fprintf(file, "#include \"%s_cat.h\"\n\n", FilePart(CatalogName) );
  632.                 fprintf(file, "extern char* %s(int);\n", GetString);
  633.                 fprintf(file, "\nchar *%s(int ref)\n{\n", GetMBString);
  634.                 fprintf(file, "\tchar *aux;\n\n");
  635.                 fprintf(file, "\taux = %s(ref);\n", GetString);
  636.                 fprintf(file, "\tif (aux[1] == '\\0') return(&aux[2]);\n");
  637.                 fprintf(file, "\telse                return(aux);\n}\n");
  638.             }
  639.             fprintf(file, "\nstruct Obj%s * Create%s(void)\n", name, name);
  640.             fprintf(file, "{\n\tstruct Obj%s * Object;\n\n", name);
  641.         }
  642.         if (Declarations)
  643.         {
  644.             WriteDeclarations   (TYPEVAR_LOCAL_PTR);
  645.             WriteInitialisations(TYPEVAR_HOOK    );
  646.         }
  647.         if (Env) fprintf(file, "\n\tif (!(Object = AllocVec(sizeof(struct Obj%s), MEMF_PUBLIC|MEMF_CLEAR)))\n\t\treturn(NULL);\n", name);
  648.         if (Declarations)
  649.         {
  650.             WriteInitialisations(TYPEVAR_PTR      );
  651.                 WriteInitialisations(TYPEVAR_BOOL     );
  652.                 WriteInitialisations(TYPEVAR_INT      );
  653.                 WriteInitialisations(TYPEVAR_STRING   );
  654.                 WriteInitialisations(TYPEVAR_TABSTRING);
  655.         }
  656.         if (Code) WriteCode();
  657.         if (Env)
  658.         {
  659.             fprintf(file, "\n\tif (!Object->%s)\n\t{\n\t\tFreeVec(Object);", name);
  660.             fprintf(file, "\n\t\treturn(NULL);\n\t}\n");
  661.         }
  662.         if (Notifications) WriteNotify();
  663.         if (Env)
  664.         {
  665.             fprintf(file, "\n\treturn(Object);\n}\n");
  666.             fprintf(file, "\nvoid Dispose%s(struct Obj%s * Object)\n{\n", name, name);
  667.             fprintf(file, "\tMUI_DisposeObject(Object->%s);\n", name);
  668.             fprintf(file, "\tFreeVec(Object);\n}\n");
  669.         }
  670.         fclose(file);
  671.     }
  672.     else printf("Unable to open GUI-File !\n");
  673. }
  674.  
  675. /* Create a file where are the external variables and functions declarations */
  676. BOOL WriteExternalFile( void )
  677. {
  678.     int    i;
  679.     ULONG    length, type;
  680.     BPTR    TMPfile;
  681.     char    *adr_file = NULL;
  682.     __aligned struct  FileInfoBlock   Info;
  683.     BOOL    bool_aux = FALSE;
  684.     char    *varname;
  685.     BOOL    result = FALSE;
  686.  
  687.     /* If the file already exists, we load it in memory */
  688.     if (TMPfile = Open(Externals, MODE_OLDFILE))
  689.     {
  690.         ExamineFH(TMPfile, &Info);
  691.         length = Info.fib_Size;
  692.         adr_file = AllocVec(length+1, MEMF_PUBLIC|MEMF_CLEAR);
  693.         Read( TMPfile, adr_file, length);
  694.         adr_file[length] = '\0';
  695.         Close(TMPfile);
  696.     }
  697.     if (file = fopen(Externals, "a+"))
  698.     {
  699.         for(i=0;i<varnb;i++)
  700.         {
  701.             MB_GetVarInfo (i,
  702.                        MUIB_VarType, &type,
  703.                        MUIB_VarName, &varname,
  704.                     TAG_END
  705.                      );
  706.             switch(type)    /* if the declaration doesn't exist, we generate it */
  707.             {
  708.             case TYPEVAR_EXTERNAL:
  709.                 if (adr_file) bool_aux = (strstr(adr_file,varname)!=NULL);
  710.                 if (!bool_aux) fprintf(file, "extern int %s;\n", varname);
  711.                 break;
  712.             case TYPEVAR_HOOK:
  713.                 if (adr_file) bool_aux = (strstr(adr_file,varname)!=NULL);
  714.                 if (!bool_aux) fprintf(file, "extern void %s( Object* );\n", varname);
  715.                 break;
  716.             }
  717.         }
  718.         fclose(file);
  719.     }
  720.     if (adr_file) FreeVec(adr_file);
  721.     if (TMPfile = Open(Externals, MODE_OLDFILE))    /* if the file is 0 bytes long : we remove it */
  722.     {
  723.         ExamineFH(TMPfile, &Info);
  724.         Close(TMPfile);
  725.         length = Info.fib_Size;
  726.         if (length == 0) DeleteFile(Externals);
  727.         else         result = TRUE;
  728.     }
  729.     return(result);
  730. }
  731.  
  732. void End(void)
  733. {
  734.     MB_Close();        /* Free Memory and Close Temporary Files */
  735.     if (MUIBBase) CloseLibrary(MUIBBase);    /* Close Library */
  736.     if (DOSBase) CloseLibrary(DOSBase);
  737. }
  738.  
  739. int main()
  740. {
  741.     /* Open MUIBuilder Library */
  742.     MUIBBase = OpenLibrary("muibuilder.library", 0);
  743.  
  744.     /* Open Dos Library */
  745.     DOSBase  = OpenLibrary("dos.library", 0);
  746.  
  747.     /* exit if it can't open */
  748.     if ((!MUIBBase)||(!DOSBase))
  749.     {
  750.         printf("Unable to open a library\n");
  751.         exit(20);
  752.     }
  753.  
  754.     /* exit if we can't init the Library */
  755.     if (!MB_Open())
  756.     {
  757.         printf("Unable to Get Temporary files !\n");
  758.         End();
  759.         exit(20);
  760.     }
  761.  
  762.     Init();
  763.  
  764.     if (Declarations)     WriteHeaderFile();
  765.     if (Env)         ExternalExist = WriteExternalFile();
  766.                 WriteGUIFile();
  767.  
  768.     End();
  769.  
  770.     exit(0);
  771. }
  772.